package com.ias.agdis_r.adapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;

import com.ias.agdis_r.R;
import com.ias.agdis_r.bean.user.UserInfoBean;
import com.ias.agdis_r.manager.DataBaseManager;
import com.ias.agdis_r.manager.JavaBeanManager;
import com.ias.agdis_r.util.S;
import com.ias.agdis_r.util.date.PrettyDataFormaterUtil;

import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 
 * @author qibo.yu
 * @version 2015年8月21日 上午10:43:45
 **/
public class MessageListAdapter extends CursorAdapter {

	private SimpleDateFormat dateFormat;
	
	private Context context;

	public MessageListAdapter(Context context, Cursor c, boolean autoRequery) {
		super(context, c, autoRequery);
		this.context = context;
		this.dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.CHINA);
	}

	public MessageListAdapter(Context context, Cursor c, int flags) {
		super(context, c, flags);
		this.dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.CHINA);
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		return LayoutInflater.from(context).inflate(R.layout.item_message, parent, false);
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		LinearLayout contentView = (LinearLayout) view.findViewById(R.id.msg_content);
		if (S.isNotEmpty(cursor.getString(1))) {
			bindMessageReceivedView(contentView, cursor);
		} else {
			bindMessageSentView(contentView, cursor);
		}

	}

	private void bindMessageReceivedView(LinearLayout view, Cursor cursor) {
		bindMessageView(view, cursor, false);

	}

	private void bindMessageSentView(LinearLayout view, Cursor cursor) {
		bindMessageView(view, cursor, true);

	}

	private void bindMessageView(LinearLayout view, Cursor cursor, boolean isSentOut) {
		FrameLayout.LayoutParams layoutParams = (android.widget.FrameLayout.LayoutParams) view.getLayoutParams();
		layoutParams.gravity = isSentOut ? Gravity.END : Gravity.START;

		View msg_header = view.findViewById(R.id.msg_panel_head);
		view.findViewById(R.id.msg_panel_indicator).setVisibility(isSentOut ? View.GONE : View.VISIBLE);

		view.setBackgroundResource(isSentOut ? R.drawable.selector_msg_item_out : R.drawable.selector_msg_item);

		String stm = null, sem = null, mtn = null, fln = null, txt = null, ack = null, mti = null, fst = null,
				readed = null;

		stm = cursor.getString(0);
		txt = cursor.getString(4);
		fln = cursor.getString(3);
		if (!isSentOut) {
			sem = cursor.getString(1);
			mtn = cursor.getString(2);
			ack = cursor.getString(5);
			mti = cursor.getString(6);

			fst = cursor.getString(9);
			readed = cursor.getString(10);

		}

		String time = stm;
		try {
			time = PrettyDataFormaterUtil.formateDate(dateFormat.parse(stm), context);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		((TextView) view.findViewById(R.id.msg_txt)).setText(txt);
		((TextView) view.findViewById(R.id.msg_time)).setText(time);

		if (!isSentOut) {

			msg_header.setVisibility(View.VISIBLE);

			((TextView) view.findViewById(R.id.msg_name)).setText(sem);

			TextView subTitle = (TextView) view.findViewById(R.id.msg_sub_title);
			if (S.isEmpty(fln)) {
//				if (S.isNotEmpty(mtn)) {
//					subTitle.setText("[" + mtn + "]");
//				}
			} else {
//				if (S.isEmpty(mtn)) {
					if(S.isNotEmpty(fln)){
						subTitle.setVisibility(View.VISIBLE);
						subTitle.setText(context.getString(R.string.MLA_1) + fln + "]");
					}else{
						subTitle.setVisibility(View.GONE);
					}

//				}
//				subTitle.setText("[" + mtn + ":" + fln + "]");
			}

			// 指示器
			ViewGroup indicator = (ViewGroup) view.findViewById(R.id.msg_panel_indicator);
			View viewReply = indicator.findViewById(R.id.msg_reply);
			View viewReceipt = indicator.findViewById(R.id.msg_receipt);

			indicator.setVisibility(View.VISIBLE);
			if (needAck(ack)) {
				viewReceipt.setVisibility(View.VISIBLE);
				if (ackOk(fst)) {
					viewReceipt.setBackgroundResource(R.drawable.message_receipted);
				} else {
					viewReceipt.setBackgroundResource(R.drawable.message_unreceipt);
				}

			} else {
				viewReceipt.setVisibility(View.GONE);
			}

			if (canReply(mti)) {
				viewReply.setVisibility(View.VISIBLE);
				if (isReplied(readed)) {
					viewReply.setBackgroundResource(R.drawable.message_replied);
				} else {
					viewReply.setBackgroundResource(R.drawable.message_unreply);
				}
			} else {
				viewReply.setVisibility(View.GONE);
			}
		}else{
//			msg_header.setVisibility(View.VISIBLE);
			UserInfoBean userInfo = JavaBeanManager.getInstance().getUserinfo();
			if(userInfo!=null&&userInfo.getEnm()!=null) {

				((TextView) view.findViewById(R.id.msg_name)).setText(userInfo.getEnm());
			}
			TextView subTitle = (TextView) view.findViewById(R.id.msg_sub_title);
			if(S.isNotEmpty(fln)){
				subTitle.setVisibility(View.VISIBLE);
				subTitle.setText(context.getString(R.string.MLA_1)+ fln +"]");
			}else{
				subTitle.setVisibility(View.GONE);
			}
//			else{
//				msg_header.setVisibility(View.GONE	);
//			}
		}

	}

	private boolean isReplied(String readed) {
		if ("1".equals(readed) || "true".equals(readed)) {
			return true;
		}
		return false;
	}

	private boolean ackOk(String fst) {
		if ("Y".equals(fst)) {
			return true;
		}
		return false;
	}

	private boolean needAck(String ack) {
		if ("Y".equals(ack)) {
			return true;
		}
		return false;
	}

	private boolean canReply(String mti) {
		if ("02".equals(mti) || "10".equals(mti)) {
			return true;
		}
		return false;
	}

}
